package short_semester.questionnaire_planet.controller;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import short_semester.questionnaire_planet.dao.FillBlankQuestionDao;
import short_semester.questionnaire_planet.dao.FillBlankQuestionRecordDao;
import short_semester.questionnaire_planet.dao.QuestionnaireDao;
import short_semester.questionnaire_planet.dao.QuestionnaireRecordDao;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.request.analyse.CrossBody;
import short_semester.questionnaire_planet.response.AnalyseController.*;
import short_semester.questionnaire_planet.response.CommonResponse;
import short_semester.questionnaire_planet.MyUtil.ExcelUtil;
import short_semester.questionnaire_planet.service.analyse.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/api")
public class AnalyseController {
    @Autowired
    QuestionnaireDao questionnaireDao;

    @Autowired
    QuestionnaireRecordService questionnaireRecordService;
    
    @Autowired
    FillBlankQuestionRecordDao fillBlankQuestionRecordDao;
    
    @Autowired
    FillBlankQuestionDao fillBlankQuestionDao;
    
    @Autowired
    QuestionnaireRecordDao questionnaireRecordDao;
    
    @Autowired
    CrossService crossService;
    
    @Autowired
    ExcelUtil excelUtil;

    @PostMapping("questionnaire/getInfo")
    public CommonResponse getQuestionnaireAnalyse(@RequestParam Integer questionnaireId){
        CommonResponse response = new CommonResponse();
        
        try {
            response.setData(questionnaireRecordService.getAnalyseInfoResponse(questionnaireId));
        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    @PostMapping("questionnaire/getCompletion")
    public CommonResponse getCompletion(@RequestParam Integer questionId){
        CommonResponse response = new CommonResponse();
        
        try {
            //题目
            FillBlankQuestion question = fillBlankQuestionDao.getById(questionId);
            
            //题目记录
            List<FillBlankQuestionRecord> questionRecords = fillBlankQuestionRecordDao.findByFillBlankQuestion(question);
//            System.out.println(questionRecords);

            //返回数组
            List<CompletionResponse> completions = new LinkedList<>();
            
            for(FillBlankQuestionRecord record : questionRecords){
                if(record.getQuestionnaireRecord().getIsSubmitted())
                    completions.add(new CompletionResponse(record));
            }
            Map<String, List<CompletionResponse>> map = new HashMap<>();
            map.put("completions",completions);
            response.setData(map);
            
        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    @PostMapping("questionnaire/usersRecords")
    public CommonResponse getUsersRecords(@RequestParam Integer questionnaireId){
        CommonResponse response = new CommonResponse();
        try {
            response.setData(questionnaireRecordService.getRecordsResponse(questionnaireId));
        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        return response;
    }
    
    @PostMapping("questionnaire/userRecord")
    public CommonResponse getUserRecord(@RequestParam Integer recordId){
        CommonResponse response = new CommonResponse();
        
        try {
            response.setData(questionnaireRecordService.getRecordResponse(recordId));
        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    @GetMapping("questionnaireRecord/download")
    public void downloadQuestionnaireRecord(HttpServletResponse response,@RequestParam Integer questionnaireId) throws IOException {
        Questionnaire questionnaire = questionnaireDao.getById(questionnaireId);
        Workbook book = excelUtil.QuestionnaireRecordToExcel(questionnaire);
        String filename = questionnaire.getTitle()+"_原始答卷.xlsx";

        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        // response.setContentType("application/force-download");
        response.setHeader("Content-Disposition", "attachment;fileName=" +   java.net.URLEncoder.encode(filename,"UTF-8"));
        book.write(response.getOutputStream());
        ((SXSSFWorkbook)book).dispose();
    }
    
    @PostMapping("questionnaire/cross")
    public CommonResponse cross(@RequestBody CrossBody body){
        CommonResponse response = new CommonResponse();
        
        try {
            response.setData(crossService.cross(body));
        }catch (Exception e){
            e.printStackTrace();;
            response.error(e.getMessage());
        }
        
        return response;
    }
}
